package org.desktop.web.client.users.window;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.gui.models.AbstractWindowForm.OperationType;
import org.desktop.web.client.images.Resources;
import org.desktop.web.client.user.UserService;
import org.desktop.web.client.user.UserServiceAsync;
import org.desktop.web.client.user.form.UserFormWindow;
import org.desktop.web.client.users.grid.UsersGrid;
import org.desktop.web.client.util.Util;
import org.desktop.web.shared.Response;
import org.desktop.web.shared.domain.entities.User;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class UsersWindow extends AbstractWindow {

	private FramedPanel panel;

	private UsersGrid usersGrid;

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "USERS";
	}

	@Override
	public void init() {

		this.setWidth(790);
		this.setHeight(480);
		this.panel = new FramedPanel();
		this.panel.setHeaderVisible(false);

		this.usersGrid = new UsersGrid(20);

		TextButton addUser = new TextButton();
		addUser.setIcon(Resources.IMAGES.add());
		addUser.setText("Add user");
		TextButton removeUser = new TextButton("Remove");
		removeUser.setIcon(Resources.IMAGES.delete());
		
		ToolBar userToolBar = new ToolBar();
		userToolBar.add(addUser);
		userToolBar.add(removeUser);


		VerticalLayoutContainer mainPanel = new VerticalLayoutContainer();

		VerticalLayoutContainer userContainer = new VerticalLayoutContainer();
//		VerticalLayoutContainer subProjectContainer = new VerticalLayoutContainer();

		userToolBar.setLayoutData(new VerticalLayoutData(1, -1));
		userContainer.add(userToolBar);
		// BorderLayoutContainer con = new BorderLayoutContainer();

		ContentPanel usersPanel = new ContentPanel();
		usersPanel.setHeaderVisible(false);
		usersPanel.add(usersGrid.asWidget());

//		ContentPanel subProjectPanel = new ContentPanel();
//		subProjectPanel.setHeaderVisible(false);
//		subProjectPanel.add(spGrid.asWidget());

		// gli eventi sui button della toolbar vanno aggiunti inquesto punto,
		// dopo la chiamata al metodo asWidget

//		final Grid<SubProject> child = spGrid.getGrid();
		final Grid<User> parent = usersGrid.getGrid();

		

		addUser.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				UserFormWindow form = new UserFormWindow(OperationType.INSERT,usersGrid);
//				ProjectFormWindow form = new ProjectFormWindow(pGrid);
				form.init();
				form.show();

			}
		});
		removeUser.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (parent.getSelectionModel().getSelectedItem() == null) {
					AlertMessageBox b = new AlertMessageBox("Error",
							"Selezionare la riga da eliminare");
					b.setIcon(AlertMessageBox.ICONS.warning());
					b.setModal(true);
					b.show();
					return;
				}
				if(parent.getSelectionModel().getSelectedItem().getRole().getCodRole().equalsIgnoreCase("ADM_R")){
					Util.showAlert("Attenzione!!", "L'utente con questo ruolo non puo' essere eliminato", MessageBox.ICONS.info());
					return;
				}
				ConfirmMessageBox cmb = new ConfirmMessageBox("", "Sei sicuro di voler eliminare l'utente?");
				cmb.setPredefinedButtons(PredefinedButton.OK,PredefinedButton.CANCEL);
				cmb.getButton(PredefinedButton.OK).addSelectHandler(new SelectHandler() {
					
					@Override
					public void onSelect(SelectEvent event) {
						UserServiceAsync service = GWT.create(UserService.class);
						service.delete(Util.getHeaders(),parent.getSelectionModel().getSelectedItem(),new AsyncCallback<Response>() {
							
							@Override
							public void onSuccess(Response result) {
								if(!result.isSuccess()){
									JSONValue json = JSONParser.parseStrict(result.getJsonMetadata());
									Util.showAlert("Error",json.isObject().get("ERROR").isString().stringValue(),MessageBox.ICONS.error());
									return;
								}
								usersGrid.load();
							}
							
							@Override
							public void onFailure(Throwable arg0) {
								
							}
						});
					}
				});
				cmb.show();
					

				

			}
		});
		VerticalLayoutData vldUsersGrid = new VerticalLayoutData(1.0, 1.0,
				new Margins(5, 0, 5, 0));
		VerticalLayoutData vldSubProjectGrid = new VerticalLayoutData(1.0, 0.5);

		userContainer.add(usersPanel, new VerticalLayoutData(1, 1));
		userContainer.add(usersGrid.getPagingToolBar(), new VerticalLayoutData(
				1, -1));
//		subProjectContainer.add(subProjectPanel, new VerticalLayoutData(1, 1));
//		subProjectContainer.add(spGrid.getPagingToolBar(),
//				new VerticalLayoutData(1, -1));

		mainPanel.add(userContainer, vldUsersGrid);
//		mainPanel.add(subProjectContainer, vldSubProjectGrid);

		this.panel.add(mainPanel);
		this.add(panel);
		// TODO Auto-generated constructor stub
	}

}
